package com.eight.paper.admin.controller;

import com.eight.common.annotation.Log;
import com.eight.common.constant.ExamConstants;
import com.eight.common.core.controller.BaseController;
import com.eight.common.core.domain.AjaxResult;
import com.eight.common.core.page.TableDataInfo;
import com.eight.common.core.redis.RedisCache;
import com.eight.common.enums.BusinessType;
import com.eight.common.utils.poi.ExcelUtil;
import com.eight.common.utils.uuid.IdUtils;
import com.eight.paper.admin.domain.ExamPaper;
import com.eight.paper.admin.domain.model.ExamPaperBody;
import com.eight.paper.admin.domain.model.ExamPaperReponseBody;
import com.eight.paper.admin.domain.model.MonitorResult;
import com.eight.paper.admin.domain.model.enums.AnswerShowTypeEnum;
import com.eight.paper.admin.service.IExamPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考试试卷Controller
 * 
 * @author eight
 * @date 2023-10-31
 */
@Api(value = "出卷管理", tags = "考试试卷")
@RestController
@RequestMapping("/paper/paper")
public class ExamPaperController extends BaseController
{
    @Autowired
    private IExamPaperService examPaperService;
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询考试试卷列表
     */
    @ApiOperation(value = "查询考试试卷列表")
    @PreAuthorize("@ss.hasPermi('paper:paper:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamPaper examPaper)
    {
        startPage();
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        return getDataTable(list);
    }

    /**
     * 导出考试试卷列表，商业版不再使用
     */
    @ApiOperation(value = "导出考试试卷列表")
    @PreAuthorize("@ss.hasPermi('paper:paper:export')")
    @Log(title = "考试试卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamPaper examPaper)
    {
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        ExcelUtil<ExamPaper> util = new ExcelUtil<ExamPaper>(ExamPaper.class);
        util.exportExcel(response, list, "考试试卷数据");
    }

    /**
     * 获取考试试卷详细信息
     */
    @ApiOperation(value = "获取考试试卷详细信息")
    @PreAuthorize("@ss.hasPermi('paper:paper:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(examPaperService.selectExamPaperById(id));
    }

    /**
     * 创建考试试卷
     */
    @ApiOperation(value = "创建考试试卷")
    @PreAuthorize("@ss.hasPermi('paper:paper:add')")
    @Log(title = "考试试卷", businessType = BusinessType.INSERT)
    @PostMapping("/createPaper")
    public AjaxResult createPaper(@RequestBody ExamPaperBody examPaperBody)
    {
        examPaperService.createExamPaper(examPaperBody);
        return success();
    }

    /**
     * 编辑考试试卷
     */
    @ApiOperation(value = "编辑考试试卷")
    @PreAuthorize("@ss.hasPermi('paper:paper:add')")
    @Log(title = "考试试卷", businessType = BusinessType.INSERT)
    @PostMapping("/updatePaper")
    public AjaxResult updatePaper(@RequestBody ExamPaperBody examPaperBody)
    {
        examPaperService.updatePaper(examPaperBody);
        return success();
    }

    /**
     * 复制考试试卷
     */
    @ApiOperation(value = "创建考试试卷")
    @PreAuthorize("@ss.hasPermi('paper:paper:copy')")
    @Log(title = "考试试卷", businessType = BusinessType.INSERT)
    @GetMapping("/copyPaper/{id}")
    public AjaxResult copyPaper(@PathVariable Integer id)
    {
        examPaperService.copyPaper(id);
        return success();
    }

    /**
     * 修改考试试卷
     */
    @ApiOperation(value = "修改考试试卷")
    @PreAuthorize("@ss.hasPermi('paper:paper:edit')")
    @Log(title = "考试试卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamPaper examPaper)
    {
        return toAjax(examPaperService.updateExamPaper(examPaper));
    }

    /**
     * 删除考试试卷
     */
    @ApiOperation(value = "删除考试试卷")
    @PreAuthorize("@ss.hasPermi('paper:paper:remove')")
    @Log(title = "考试试卷", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(examPaperService.deleteExamPaperByIds(ids));
    }

    /**
     * 抽题组卷，获取试题信息
     */
    @ApiOperation(value = "抽题组卷，获取试题信息")
    //@PreAuthorize("@ss.hasPermi('paper:paper:getQuestions')")
    //@Log(title = "考试试卷", businessType = BusinessType.INSERT)
    @PostMapping("/getQuestions")
    public AjaxResult getQuestions(@RequestBody ExamPaperBody examPaperBody)
    {
        ExamPaperReponseBody paper = examPaperService.getQuestions(examPaperBody, AnswerShowTypeEnum.True.getCode());
        return success(paper);
    }

    /**
     * 导出对应试卷所有考生考试列表
     */
    @ApiOperation(value = "导出对应试卷所有考生考试列表")
//    @PreAuthorize("@ss.hasPermi('paper:paper:exportPapers')")
    @Log(title = "考试试卷", businessType = BusinessType.EXPORT)
    @GetMapping("/exportPapers/{id}")
    public AjaxResult exportPapers(@PathVariable Integer id)
    {
        String uuid = IdUtils.simpleUUID();
        redisCache.setCacheObject(String.format(ExamConstants.EXAM_EXPORT_PRESET_AMOUNT,uuid), -1);
        threadPoolTaskExecutor.execute(() -> {
            try {
                examPaperService.exportPapersById(id, uuid);
            } catch (Exception e) {
                logger.error("导出试卷异常：", e);
            }
        });
        return AjaxResult.success(uuid);
    }

    @ApiOperation(value = "导出对应试卷所有考生考试进度监测")
    @PreAuthorize("@ss.hasPermi('paper:paper:exportMonitor')")
    @GetMapping("/exportMonitor/{uuid}")
    public AjaxResult exportMonitor(@PathVariable String uuid) {
        MonitorResult result = examPaperService.exportMonitor(uuid);
        return AjaxResult.success(result);
    }

    @ApiOperation(value = "下载导出文件")
    @PreAuthorize("@ss.hasPermi('paper:paper:downLoadExport')")
    @PostMapping("/downLoadExport/{uuid}")
    public void downLoadExport(HttpServletResponse response, @PathVariable String uuid) {
        examPaperService.downLoadExport(response, uuid);
    }
}
